package com.project.common.Security;

import com.project.entity.Users;
import com.project.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义UserDetails
 *
 * @author Leo高洋
 * @create 2023-01-15 15:23
 */
@Service
public class MyUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    UsersService usersService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Users users = usersService.getByUsername(username);
        if (users == null) {
            throw new UsernameNotFoundException("用户名或者密码错误！");
        }
        return new User(users.getUsername(), users.getPassword(), getUserAuthority());
    }

    public List<GrantedAuthority> getUserAuthority() {
        return new ArrayList<>();
    }
}
